<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start('ob_gzhandler'); else ob_start(); ?>
<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
//paths for GWTCommerce stuff
$include_path = ini_get('include_path');
ini_set('include_path', 'php:' . $include_path);
      
require_once('includes/classes/JSON.php');
require_once('includes/components/ComponentController.php');
require_once('includes/functions/functions.php');
require_once('includes/functions/cache_functions.php');

//handles is used in a multi-request, it's all the components having information requested.
$handles = 'CategoriesComponent,ManufacturersComponent,CurrenciesComponent,LanguagesComponent,AccountComponent,InformationComponent,NewProductsComponent,BestSellersComponent,WhatsNewComponent,OrderedProductsComponent,RandomReviewComponent,SpecialsComponent';


//Used when information has gone through a 3rd Party Authorization
$gwtAction = $_REQUEST['gwt_action'];
if ($gwtAction == 'ProcessPayment')
{
	$value = array();
	//reverse the 'gwt_' and other request parameters so that they are usable in Components and Modules
	//when finished, the parameters specific to the 3rd party will start with 'gwt_' and the parameters
	//specific to gwt commerce will have the 'gwt_' removed.
	foreach ($_REQUEST as $reqkey => $reqvalue)
    	{
		$pos = strpos($reqkey, 'gwt_');
		if ($pos === false || $pos != 0) {
			$value['gwt_' . $reqkey] = $reqvalue;
		}
		else {
			$value[substr($reqkey, 4)] = $reqvalue;
		}
	}
	$value['token'] = CheckoutComponent;
	$value['third_party_payment_process'] = true;
	$handles .= ',CheckoutComponent';
}
else
{
	$value = $_REQUEST;
	$value['third_party_payment_process'] = false;
}

//Components to display on return

$token = prepare_input($value['token']);

//Important parameters
$action = prepare_input($value['action']);
if ($action == null) $action = 'query';
$value{'action'} = $action;
$languageId = (int)$value['languageId'];
$testSSL = (boolean)$value['testSSL'];
//handle is the PHP Component to request information from
$handle = 'MultipleResultsComponent';
//value is an array of parameters sent to the component(s).
$value{'handle'} = $handle;

if ($gwtAction != 'ProcessPayment')
{
	$categoryId = (int)$value['categoryId'];
	$oldCategoryId = $value['cPath'];
	if ($categoryId == 0 && $oldCategoryId != null && strlen($oldCategoryId) > 0)
	{
		$pos = strrpos($oldCategoryId, "_");
		if ($pos === false) {
			$categoryId = (int)$oldCategoryId;
		}
		else {
			$categoryId = (int)substr($oldCategoryId, $pos+1);
		}
		$value['categoryId'] = $categoryId;
	}
	  

	$productId = (int)$value['productId'];
	$oldProductId = (int)$value['products_id'];
	if ($productId == 0 && $oldProductId > 0)
	{
		$productId = $oldProductId;
		$value['productId'] = $productId;
	}

	$orderId = (int)$value['orderId'];
	if ($orderId > 0)
	{
		$token = 'OrderComponent--' . $orderId;
	}
	else if ($categoryId > 0 && $productId == 0)
	{
		$token = 'AdvancedSearchResultsComponent--' . $categoryId . '--0--false--true--false--false--0--0--*--*--0--pd.products_name';
	}
	else if ($productId > 0)
	{
		$token = 'ProductComponent--' . $productId . '--' . $categoryId . '--0';
		$handles .= ',ProductComponent';
	}
}

$value{'handles'} = $handles;
$rvalue = actionComponent($value, $action);
	
$accountId = $rvalue{'AccountComponent'}{0}{'id'};

$mvalue{'GWTCommerceMetaData'}{'token'}= $token;
$mvalue{'GWTCommerceMetaData'}{'testSSL'}= $testSSL;
$mvalue{'GWTCommerceMetaData'}{'store_name_address'}= nl2br(STORE_NAME_ADDRESS);
$mvalue{'GWTCommerceMetaData'}{'home_url'}= HOME_URL;
$mvalue{'GWTCommerceMetaData'}{'information_component_is_horizontal'}=returnBoolean(INFORMATION_COMPONENT_IS_HORIZONTAL);
if ($gwtAction == 'ProcessPayment')
{
	$mvalue{'GWTCommerceMetaData'}{'third_party_payment_process'}=true;
}

$rvalue = array_merge($mvalue, $rvalue);

$json = new Services_JSON();
$jsonStr = $json->encode($rvalue);
//$jsonStr = json_encode($rvalue);

$loginFormAction = 'index.php';
$codesvr = $_GET['gwt_codesvr'];
if ($codesvr != null && strlen($codesvr) > 0) {
	$loginFormAction .= '?gwt.codesvr=' . $codesvr;
}
?>
<!DOCTYPE HTML PUBLIC >
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="gwt:property" content="locale=<?php echo $rvalue{'LanguagesComponentMetaData'}{'current_code'} ?>">
    <title><?php echo STORE_NAME ?></title>
    <link rel="stylesheet" href="gwtCommerce/GWTCommerce.css"/>
    <link rel="stylesheet" href="gwtCommerce/GWTCommerceWait.css"/>
  </head>
  <body>
    <!-- OPTIONAL: include this if you want history support -->
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>

<form id="loginForm" class="LoginPanel" action="<?php echo $loginFormAction?>" style="display: none;" method="post">
  <table class="AccountComponentSubPanel" style="width: 100%;">
    <tbody>
      <tr>
        <td style="white-space: nowrap;">E-Mail Address:</td>
        <td><input class="gwt-TextBox" type="text" tabindex="0" name="email_address"/></td>
        <td class="WarningLabel AccountComponentWarningLabel" width="100%" style="white-space: nowrap;"></td>
      </tr>
      <tr>
        <td style="white-space: nowrap;">Password:</td>
        <td><input class="gwt-PasswordTextBox" type="password" tabindex="0" name="password"/></td>
        <td class="WarningLabel AccountComponentWarningLabel" width="100%" style="white-space: nowrap;"></td>
      </tr>
      <tr>
        <td> </td>
      </tr>
      <tr>
        <td><input type="submit" value="sign in" /></td>
      </tr>
    </tbody>
  </table>
  <input type="hidden" name="action" value="LoginAccount"/>
  <input type="hidden" name="cart" value=""/>
  <input type="hidden" name="token" value=""/>
</form>

  <noscript>

<table cellspacing="0" cellpadding="0" class="TopPanel"><tbody>
  <tr><td align="left" style="vertical-align: top;">
    <div class="MiddleComponent HeaderComponent" style="">
      <table class="MiddleComponentPanel HeaderComponentPanel"><tbody>
        <tr><td align="center" class="MiddleComponentBody HeaderComponentBody">
          <table cellspacing="0" cellpadding="0" class="HeaderComponentBodyPanel"><tbody><tr>
            <td width="" height="" align="left" style="vertical-align: top;" rowspan="1">
              <table cellspacing="0" cellpadding="0" class="HeaderComponentLeftPanel"><tbody><tr><td align="left" style="vertical-align: top;"><img class="gwt-Image" src="gwtCommerce/images/HeaderComponent/en/logo.jpg"/></td></tr></tbody></table>
            </td>
          </tr></tbody></table>
        </td></tr>
      </tbody></table>
    </div>
  </td></tr>
  <tr><td align="left" style="vertical-align: top;">
    <div class="MiddleComponent BreadcrumbComponent" style="">
      <table class="MiddleComponentPanel BreadcrumbComponentPanel"><tbody>
        <tr><td class="MiddleComponentTitle BreadcrumbComponentTitle">
          <table cellspacing="0" cellpadding="0" class="BreadcrumbComponentDockPanel"><tbody>
            <tr><td width="" height="" align="left" style="vertical-align: top;" rowspan="1">
              <table cellspacing="0" cellpadding="0" class="BreadcrumbComponentTitlePanel"><tbody>
                <tr>
                  <td align="left" style="vertical-align: top;"><a tabindex="0" class="gwt-Anchor BreadcrumbComponentLink" href="http://www.eatlocalfood.com/">Home</a></td>
                  <td align="left" style="vertical-align: top;"><span class="gwt-InlineHTML BreadcrumbComponentDelimiter">»</span></td>
                  <td align="left" style="vertical-align: top;"><span class="gwt-InlineLabel BreadcrumbComponentLink">Store</span></td>
                  <td align="left" style="vertical-align: top;"><span class="gwt-InlineHTML BreadcrumbComponentDelimiter">»</span></td>
                  <td align="left" style="vertical-align: top;"><span class="gwt-InlineLabel BreadcrumbComponentLink BreadcrumbComponentDeadLink">Welcome to the <?php echo STORE_NAME ?> Store!</span></td>
                </tr>
              </tbody></table>
            </td></tr>
          </tbody></table>
        </td></tr>
      </tbody></table>
    </div>
  </td></tr>
</tbody></table>
<div>
<?php
if ($productId < 1)
{
    $cats = $rvalue{'CategoriesComponent'};
    for($i = 0; $i < sizeof($cats); ++$i)
    {
        if ($cats[$i]['parent_id'] == $categoryId)
        {
                echo('<a href="index.php?categoryId='.$cats[$i]['id'].'">'.$cats[$i]['name'].'</a><br/>');
        }
	else if ($cats[$i]['id'] == $categoryId)
	{
		$prods = $cats[$i]['products'];
    		for($j = 0; $j < sizeof($prods); ++$j)
    		{
                	echo('<a href="index.php?productId='.$prods[$j]['id'].'">'.$prods[$j]['name'].'</a><br/>');
		}
	}
    }

    if ($categoryId < 1)
    {
        $ic = $rvalue{'InformationComponent'};
        for($i = 0; $i < sizeof($ic); ++$i)
        {
            echo('<p class="InformationDescriptionComponentTitle">'.$ic[$i]['title'].'</p>');
            $desc = $ic[$i]['description'];
            $pos = strpos($desc, 'http');
    
            if ($pos === false) {
                echo('<p>'.$ic[$i]['description'].'</p>');
            } 
            else {
                echo('<a href="'.$ic[$i]['description'].'">'.$ic[$i]['description'].'</a>');
            }
        }
    }
}
else
{
	$prod = $rvalue{'ProductComponent'};

	echo('<p class="InformationDescriptionComponentTitle">'.$prod[0]['name'].'</p>');
	echo('<p>Model: ['.$prod[0]['model'].']</p>');
	echo('<p>Price: $'.$prod[0]['price'].'</p>');
	echo('<p>Discount Price: $'.$prod[0]['discount_price'].'</p>');
	echo('<p>Special Price: $'.$prod[0]['special_price'].'</p>');
	echo('<p>'.$prod[0]['description'].'</p>');
        echo('<a href="'.$prod[0]['url_id'].'">'.$prods[$j]['url'].'</a><br/>');
	echo('<p>Date Added: '.$prod[0]['date_added'].'</p>');
} 
?>
</div>
  </noscript>
    <script>var initial_json = "<?php echo $jsonStr ?>";</script>
    <script type="text/javascript" language="javascript" src="gwtCommerce/gwtCommerce.nocache.js"></script>
  </body>
</html>
